/* eslint-disable @typescript-eslint/no-explicit-any */
import { instance } from '../../common/network';

export async function importCommonConfig() {
    const res = await import('../../config/index.js' as unknown as string);
    return res;
}

instance.interceptors.request.use(
    async config => {
        const curEnv = process.env.APP_DEPLOY_ENV as string;
        const curConfig = await importCommonConfig();
        config.baseURL = curConfig[curEnv].apiHost;
        return config;
    },
    error => {
        return Promise.reject(error);
    },
);

import { AxiosRequestConfig } from 'axios';

export const createRequest = <TReq, TResp = any>(
    _: string,
    requestConfigCreator: (args: TReq) => AxiosRequestConfig,
) => {
    return async (args: TReq) => {
        const res = await instance.request<TResp>(requestConfigCreator(args));
        return res.data;
    };
};

export default {
    instance,
};
